/*
 * @Author: ziqi jhzq12345678
 * @Date: 2024-09-24 09:02:43
 * @LastEditors: ziqi jhzq12345678
 * @LastEditTime: 2025-01-07 16:18:10
 * @FilePath: /pornhub_app/lib/src/views/search/search_history.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:pornhub_app/views/search/search_controller.dart';

import 'package:pornhub_app/routes/routes.dart';

import 'package:pornhub_app/utils/color.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class SearchHistory extends StatefulWidget {
  const SearchHistory({
    super.key,
  });

  @override
  State<SearchHistory> createState() => _SearchHistory();
}

class _SearchHistory extends State<SearchHistory> {
  final searchvc = Get.find<SearchPageController>();

  void handleClear() {
    searchvc.clearHistory();
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (searchvc.history.isEmpty) return const SizedBox.shrink();
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(padding: EdgeInsets.only(top: 20.w)),
        Flex(
          direction: Axis.horizontal,
          children: [
            Expanded(
                child: Text(
              '历史搜索',
              style: TextStyle(
                color: COLOR.hexColor('#ffffff'),
                fontSize: 14.w,
              ),
            )),
            GestureDetector(
              onTap: handleClear,
              child: Text(
                '清空',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 14.w,
                    fontWeight: FontWeight.w600),
              ),
            )
          ],
        ),
        Padding(padding: EdgeInsets.only(bottom: 14.w)),
        Obx(() {
          return Wrap(
            spacing: 10.w,
            runSpacing: 12.w,
            alignment: WrapAlignment.start,
            children: searchvc.history
                .map((e) => GestureDetector(
                      onTap: () {
                        searchvc.addHistory(e);
                        Get.toNamed(Routes.searchresult,
                            arguments: {'word': e});
                      },
                      child: Container(
                        height: 23.w,
                        padding: EdgeInsets.symmetric(
                            horizontal: 14.w, vertical: 3.w),
                        decoration: BoxDecoration(
                            border:
                                Border.all(color: COLOR.hexColor('#ACABB1')),
                            borderRadius: BorderRadius.circular(15.w)),
                        child: Text(
                          e,
                          style: TextStyle(
                              color: COLOR.hexColor('#CCCBCE'), fontSize: 12.w),
                        ),
                      ),
                    ))
                .toList(),
          );
        })
      ],
    );
  }
}
